// pages/getDiscount/getDiscount.js
var util = require('../../utils/util.js');
Page({
  data: {
    hasDiscount: true,
    discountItem: [], //优惠券列表
    is_own: '', //
    discountId: '' //优惠券id

  },
  onShow: function() {
    var that = this;
    util.postUrl('coupon/red_packet_list', {}, function(res) {
      if (res.data.code == 200) {
        console.log('ffffffff', res.data.datas.rpt_list);
        that.setData({
          discountItem: res.data.datas.rpt_list,
        })
      }
    });
    // this.immediatelyGet();
    console.log('qqqqqqqqqqqqq')
  },
  // 领取优惠券：
  immediatelyGet: function(e) {
    var that = this;
    let packet_id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index
    util.postUrl('member_red_packet/add_red_packet', {
      packet_id: packet_id
    }, function(res) {
      if (res.data.code == 200) {
        console.log('sssssssssd', res);
        let discountItem = that.data.discountItem;
        let is_own = discountItem[index].is_own;
        discountItem[index].is_own = !is_own;
        that.setData({
          discountItem: discountItem
        });
        wx.showToast({
          title: '已领取优惠券！',
          icon: 'none'
        })
      }
    })
  },
  usedDiscount: function(e) {
    wx.navigateTo({
      url: '../auth-info/auth-info',
    })
  },
  // 查看我的优惠券
  myDiscount: function() {
    wx.navigateTo({
      url: '../myDiscount/myDiscount',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})